package com.cuz.hot100;

import java.util.*;

public class Q49groupingAnagrams {

    public List<List<String>> groupAnagrams(String[] strs) {
        if (strs == null || strs.length == 0) {
            return Collections.emptyList();
        }
        HashMap<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            char[] tempArray = str.toCharArray();
            Arrays.sort(tempArray);
            String s = new String(tempArray);
            List<String> orDefault = map.getOrDefault(s, new ArrayList<>());
            orDefault.add(str);
            map.put(s,orDefault);
        }
       return new ArrayList<>(map.values());
    }
}
